﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Data;

namespace Manager.gui
{
	public partial class PanelRaces : UserControl
	{
		private MadiManager manager;
		public PanelRaces()
		{
			InitializeComponent();	
		}

		public void updateFields()
		{
			manager = MadiManager.get();
			listViewRaces.ItemsSource = manager.data.Races;
		}

		private void bNuevaRaza_Click(object sender, RoutedEventArgs e)
		{
			gui.DialogNewRace dlg = new gui.DialogNewRace();
			
			dlg.ShowDialog();
			if (dlg.DialogResult == true)
			{
				manager.data.Races.Add(dlg.race);
				manager.isModified = true;
			}
		}

		private void bEditarRaza_Click(object sender, RoutedEventArgs e)
		{
			Race razaEditada = (Race)listViewRaces.SelectedItem;
			if (razaEditada != null)
			{
				gui.DialogNewRace dlg = new gui.DialogNewRace(razaEditada);
				
				dlg.ShowDialog();
				if (dlg.DialogResult == true)
				{
					Race razaNueva=dlg.race;
					int posicion = manager.data.Races.IndexOf(razaEditada);
					manager.data.Races.Insert(posicion, razaNueva);
					manager.data.Races.Remove(razaEditada);
					manager.isModified = true;
				}
			}
		}

		private void bEliminarRaza_Click(object sender, RoutedEventArgs e)
		{
			Race razaEliminable = (Race)listViewRaces.SelectedItem;
			if (razaEliminable != null)
			{
				MessageBoxResult result =
					  MessageBox.Show(
						"¿Estás seguro de que deseas eliminar esta raza? Esta acción no se puede deshacer.",
						"¿Elimiar raza '" + razaEliminable.Name + "'?",
						MessageBoxButton.YesNo,
						MessageBoxImage.Warning);
				if (result == MessageBoxResult.Yes)
				{
					manager.data.Races.Remove(razaEliminable);
					manager.isModified = true;
				}
			}
		}

		private void listViewRaces_MouseDoubleClick(object sender, MouseButtonEventArgs e)
		{
			bEditarRaza_Click(sender, e);
		}
	}
}
